package com.siysoft.util.fileupload;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.siysoft.exception.FileUploadException;

public class FileUpload {
	
	private String DEST_DIR = "/resource/upload/";
	private String database_dir = "/resource/upload/";
	
	/**
	 * 文件上传
	 * @param multipartRequest MultipartHttpServletRequest接口
	 * @param fileMap 文件Map
	 * 
	 * @param local 工程绝对路径
	 * 
	 * @param userId 当前用户id
	 * 
	 * @throws FileUploadException 
	 * @return 文件上传后的服务器路径集合
	 */
	public Map<String,String> uploadFile(MultipartHttpServletRequest multipartRequest,
			Map<String, MultipartFile> fileMap, String local, String userId)
			throws FileUploadException {
		
		Map<String,String> serviceFileList = null;
		try {
			serviceFileList = new HashMap<String,String>();
			File dir = new File(local + DEST_DIR);
			if (!dir.exists()) {
				dir.mkdir();
			}

			File uploadFile;
			for (Entry<String, MultipartFile> entry : fileMap.entrySet()) {
		          
		        String resourceType = entry.getKey();
				MultipartFile multipartFile = entry.getValue();
				//获取文件后缀名
				String tempFileName = multipartFile.getOriginalFilename();
				String fileLast = tempFileName.substring(tempFileName.lastIndexOf("."));
				
				//文件名称
				Date date = new Date();
		        SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");       
		        String newfileName = userId + "_" + sdfFileName.format(date);
				newfileName = newfileName + fileLast; 
				
				//上传..
				uploadFile = new File(local + DEST_DIR + newfileName);
				multipartFile.transferTo(uploadFile);
				
				serviceFileList.put(resourceType, DEST_DIR + newfileName);

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new FileUploadException();
		}
		
		return serviceFileList;
	}
	
	public String uploadFileByBase64(String base64, String userId, HttpServletRequest request){
		Date date = new Date();
		String local = request.getServletContext().getRealPath("/"); 
        SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");       
        String newfileName = userId + "_" + sdfFileName.format(date);
		//生成唯一文件名
		String filename = newfileName + ".png";
		try {
			FileUtil.save(base64,local + DEST_DIR,filename);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return database_dir + filename;
	}
	
	public String uploadFileByBase64(String base64, String userId, String local){
		Date date = new Date();
        SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");       
        String newfileName = userId + "_" + sdfFileName.format(date);
		//生成唯一文件名
		String filename = newfileName + ".jpg";
		try {
			FileUtil.save(base64,local + DEST_DIR,filename);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return database_dir + filename;
	}
}
